﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using Lists;
using Vouchers;

namespace KeToanTongHop
{
    public partial class FrmGhiSoLaiCacChungTu : BasicForms.FrmBase
    {
        public FrmGhiSoLaiCacChungTu()
        {
            InitializeComponent();
        }


        private void FrmGhiSoLaiCacChungTu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsGhiSoLaiCacChungTu.ChungTu' table. You can move, or remove it, as needed.
            //this.chungTuTableAdapter.Fill(this.dsGhiSoLaiCacChungTu.ChungTu);
            // TODO: This line of code loads data into the 'dsGhiSoLaiCacChungTu.ChungTu' table. You can move, or remove it, as needed.
            //this.chungTuTableAdapter.Fill(this.dsGhiSoLaiCacChungTu.ChungTu);
            // TODO: This line of code loads data into the 'dsGhiSoLaiCacChungTu.ChungTu' table. You can move, or remove it, as needed.
            //this.chungTuTableAdapter.Fill(this.dsGhiSoLaiCacChungTu.ChungTu);
            rItemGridLookUpEditLoaiChungTu.DataSource = DanhMucLoaiChungTu.Instance.DataView;
            rItemLookUpEditDoiTuongNghiepVu.DataSource = DanhMucDoiTuongNghiepVu.Instance.DataView;
            #region cmbLoaiChungTu
            cmbLoaiChungTu.DataSource = DanhMucLoaiChungTu.Instance.DataView;
            cmbLoaiChungTu.DisplayColumn = "Ten";
            cmbLoaiChungTu.ValueColumn = "ID";
            cmbLoaiChungTu.AddVisibleColumn("Ma", "Mã");
            cmbLoaiChungTu.AddVisibleColumn("Ten", "Tên");
            #endregion
            cmbLoaiChungTu.EditValue = Guid.Empty;

            cmbKyBaoCao.SelectedIndex = -1;
            cmbKyBaoCao.SelectedIndex = 17;
        }

        DanhSachChungTu _DanhSachChungTu = new DanhSachChungTu();

        protected virtual void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime ngayBatDau = dtNgayBatDau.DateTime;
                DateTime ngayKetThuc = dtNgayBatDau.DateTime;
                switch (cmbKyBaoCao.Text)
                {
                    case "Tùy chọn":
                        dtNgayBatDau.Enabled = true;
                        dtNgayKetThuc.Enabled = true;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
                        break;
                    case "Cả năm":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    case "Quý I":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 1, 1);
                        ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, 3, 31, 23, 59, 59);
                        break;
                    case "Quý II":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 4, 1);
                        ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, 6, 30, 23, 59, 59);
                        break;
                    case "Quý III":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 7, 1);
                        ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, 9, 30, 23, 59, 59);
                        break;
                    case "Quý IV":
                        dtNgayBatDau.Enabled = false;
                        dtNgayKetThuc.Enabled = false;
                        ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, 10, 1);
                        ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, 12, 31, 23, 59, 59);
                        break;
                    default:
                        try
                        {
                            dtNgayBatDau.Enabled = false;
                            dtNgayKetThuc.Enabled = false;
                            string strThang = ((string)cmbKyBaoCao.SelectedItem).Substring(((string)(cmbKyBaoCao.SelectedItem)).LastIndexOf(" "));
                            int thang = int.Parse(strThang);

                            ngayBatDau = new DateTime(AccountingLibs.Config.NamTaiChinh, thang, 1);
                            ngayKetThuc = new DateTime(AccountingLibs.Config.NamTaiChinh, thang,
                                            DateTime.DaysInMonth(AccountingLibs.Config.NamTaiChinh, thang), 23, 59, 59);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Trace.WriteLine(ex.ToString());
                        }
                        break;
                }
                dtNgayBatDau.DateTime = ngayBatDau;
                dtNgayKetThuc.DateTime = ngayKetThuc;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnGhiSoLai_Click(object sender, EventArgs e)
        {
            GuiUtilities.FrmProgressBar.StartShowProgress();

            SqlConnection con = new SqlConnection(AccountingLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                    
                int count = 0;
                foreach (ChungTu chungTu in _DanhSachChungTu)
                {
                    chungTu.LoadKhoanMuc(cmd);
                    chungTu.LoadDanhSachChungTuPhuThuoc(cmd);
                    chungTu.GhiSoButToan(cmd);

                    count++;
                    GuiUtilities.FrmProgressBar.Status = string.Format("Đã xử lý {0}/{1}", count, _DanhSachChungTu.Count);
                }
            }
            catch (SqlException sqlEx)
            {
                System.Diagnostics.Trace.WriteLine(sqlEx.ToString());
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }
            GuiUtilities.FrmProgressBar.FinishShowProgress();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            GuiUtilities.FrmProgressBar.StartShowProgress();

            SqlConnection con = new SqlConnection(AccountingLibs.LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                DieuKienTimKiemChungTu dkTimKiem = new DieuKienTimKiemChungTu();
                dkTimKiem.TuNgay = new AccountingLibs.NullableDate(dtNgayBatDau.DateTime);
                dkTimKiem.DenNgay = new AccountingLibs.NullableDate(dtNgayKetThuc.DateTime);

                dkTimKiem.DieuKienKhac = string.Format("DinhKhoan = 1 AND GhiSo = 1");
                if ((Guid)cmbLoaiChungTu.EditValue != Guid.Empty)
                    dkTimKiem.DieuKienKhac += string.Format(" AND LoaiChungTuID = '{0}'", (Guid)cmbLoaiChungTu.EditValue);

                _DanhSachChungTu.LoadFromDB(dkTimKiem.BuildSqlStatement(0, 0));

                dsGhiSoLaiCacChungTu.ChungTu.Clear();
                dsGhiSoLaiCacChungTu.ChungTu.Merge(_DanhSachChungTu.DataTable);
            }
            catch (SqlException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
            }
            finally
            {
                con.Close();
            }
            GuiUtilities.FrmProgressBar.FinishShowProgress();
        }

        private void cmbLoaiChungTu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete) cmbLoaiChungTu.EditValue = Guid.Empty;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
